﻿@model CNav.Model.RenderControlListModel
@{
    ViewBag.Title = "RenderView";
}

@Html.HiddenFor(m => m.FormID)

@foreach (var item in Model.Controls)
{
    <li>
        @{
            <label class="vTop">@item.QuestionText</label>
            
            var ctrlName = string.Format("{0}_{1}", item.ControlName, item.DisplayOrder);

            if (item.ControlType == "CheckBox")
            {
                @Html.CheckBox(ctrlName, !string.IsNullOrEmpty(item.Answer) && bool.Parse(item.Answer) )
            }

            if (item.ControlType == "CheckBoxList")
            {
                if (item.DataSources != null && item.DataSources.Any())
                {
                    int i = 0;
                    var ans = string.IsNullOrEmpty(item.Answer) ? new string[item.DataSources.Count] : item.Answer.Split(',');
                    foreach (var d in item.DataSources)
                    {
                        i++;
                        @Html.CheckBox(ctrlName + i, !string.IsNullOrEmpty(ans[i-1]) && bool.Parse(ans[i-1]) , new {id = ctrlName + i})<span>&nbsp;</span>
                        <label class="labelradio" for="@string.Concat(ctrlName, i)">@d.Name</label>
                        <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
                    }
                }
            }

            if (item.ControlType == "TextBox")
            {
                @Html.TextBox(ctrlName, item.Answer)
            }

            if (item.ControlType == "TextArea")
            {
                @Html.TextArea(ctrlName, item.Answer)
            }

            if (item.ControlType == "DropDownList")
            {
                @Html.DropDownList(ctrlName, item.DataSources.Select(d => new SelectListItem {Text = d.Name, Value = d.Value, Selected=d.Value==item.Answer}))
            }
        }
    </li>
}